package com.efounder.imageloader;

import android.app.Activity;
import android.content.Context;
import androidx.fragment.app.Fragment;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestManager;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;

/**
 * Glide图片加载
 * Created by YQS on 2018/3/2.
 */

public class GlideImageLoader extends AbstractImageLoader {

    private volatile static GlideImageLoader glideImageLoader;

    public static GlideImageLoader getInstance() {
        if (glideImageLoader == null) {
            synchronized (GlideImageLoader.class) {
                if (glideImageLoader == null) {
                    glideImageLoader = new GlideImageLoader();
                }
            }
        }
        return glideImageLoader;
    }


    @Override
    @Deprecated
    public void displayImage(Context context, ImageView imageView, Object object, int errorResId, int placeHolderResId) {
        if (context != null) {
            RequestManager requestManager = Glide.with(context);
            showImageByGlide(requestManager, imageView, object, errorResId, placeHolderResId, 0);
        }
    }

    @Deprecated
    public void displayImage(Context context, ImageView imageView, Object object, int errorResId, int placeHolderResId
            , boolean enableDiskCache) {
        if (context != null) {
            RequestManager requestManager = Glide.with(context);
            showImageByGlide(requestManager, imageView, object, errorResId, placeHolderResId, 0, enableDiskCache, true);
        }
    }

    @Deprecated
    public void displayImage(Context context, ImageView imageView, Object object, int errorResId, int placeHolderResId
            , boolean enableDiskCache, boolean enableMemoryCache) {
        if (context != null) {
            RequestManager requestManager = Glide.with(context);
            showImageByGlide(requestManager, imageView, object, errorResId, placeHolderResId, 0, enableDiskCache, enableMemoryCache);
        }
    }

    @Override
    public void displayImage(Fragment fragment, ImageView imageView, Object object, int errorResId, int placeHolderResId) {
        if (fragment != null) {
            RequestManager requestManager = Glide.with(fragment);
            showImageByGlide(requestManager, imageView, object, errorResId, placeHolderResId, 0);
        }
    }

    @Override
    public void displayImage(Activity activity, ImageView imageView, Object object, int errorResId, int placeHolderResId) {
        if (activity != null && !activity.isFinishing() && !activity.isDestroyed()) {
            RequestManager requestManager = Glide.with(activity);
            showImageByGlide(requestManager, imageView, object, errorResId, placeHolderResId, 0);
        }
    }


    @Deprecated
    protected void showImageByGlide(Context context, ImageView imageView, Object object, int errorResId, int placeHolderResId, int rounderCorner) {
        if (context != null) {
            RequestManager requestManager = Glide.with(context);
            showImageByGlide(requestManager, imageView, object, errorResId, placeHolderResId, rounderCorner);
        }
    }

    /**
     * 显示图片
     *
     * @param requestManager
     * @param imageView
     * @param object
     * @param errorResId       0表示没设置
     * @param placeHolderResId 0表示没设置
     * @param rounderCorner    圆角值 等于0 表示没有  100 表示圆形
     * @param diskCache        是否启用磁盘缓存
     * @param memoryCache      是否启用内存缓存
     */
    protected void showImageByGlide(RequestManager requestManager, ImageView imageView, Object object,
                                    int errorResId, int placeHolderResId, int rounderCorner,
                                    boolean diskCache, boolean memoryCache) {
        RequestOptions options = new RequestOptions();
        if (placeHolderResId != 0) {
            options.placeholder(placeHolderResId);
        }
        if (errorResId != 0) {
            options.error(errorResId);
        }
        if (errorResId != 0) {
            options.fallback(errorResId);
        }
        if (rounderCorner > 0 && rounderCorner != 100) {
            options.transform(new RoundedCorners(rounderCorner));
        } else if (rounderCorner == 100) {
            options.circleCrop();
        }
        if (diskCache && memoryCache) {
            options.diskCacheStrategy(DiskCacheStrategy.ALL);
        }
        if (!diskCache) {
            options.diskCacheStrategy(DiskCacheStrategy.NONE);
        }
        if (!memoryCache) {
            options.skipMemoryCache(true);
        }

        if (object != null && object instanceof String &&(object.toString().startsWith("https://panserver.solarsource.cn/panserver")
        ||object.toString().startsWith("http://panserver.solarsource.cn:9692/panserver"))) {
            //TODO 由于云盘失效，这个链接的图片都已经无法加载，所以特殊处理一下
            object = "";
        }
        requestManager.load(object)
                .apply(options)
                .into(imageView);
    }

    protected void showImageByGlide(RequestManager requestManager, ImageView imageView, Object object,
                                    int errorResId, int placeHolderResId, int rounderCorner) {
        showImageByGlide(requestManager, imageView, object, errorResId, placeHolderResId, rounderCorner
                , true, true);
    }

    //自定义方法
    @Deprecated
    public void displayRoundCornerImage(Context context, ImageView imageView, Object object, int errorResId, int placeHolderResId, int roundCornor) {
        if (context != null) {
            RequestManager requestManager = Glide.with(context);
            showImageByGlide(requestManager, imageView, object, errorResId, placeHolderResId, roundCornor);
        }

    }

    public void displayRoundCornerImage(Fragment fragment, ImageView imageView, Object object, int errorResId, int placeHolderResId, int roundCornor) {
        if (fragment != null) {
            RequestManager requestManager = Glide.with(fragment);
            showImageByGlide(requestManager, imageView, object, errorResId, placeHolderResId, roundCornor);
        }

    }

    public void displayRoundCornerImage(Activity activity, ImageView imageView, Object object, int errorResId, int placeHolderResId, int roundCornor) {
        if (activity != null && !activity.isFinishing() && !activity.isDestroyed()) {
            RequestManager requestManager = Glide.with(activity);
            showImageByGlide(requestManager, imageView, object, errorResId, placeHolderResId, roundCornor);
        }
    }
}
